public class Test2 {
    public int maxLength (int[] arr) {
        // write code here
        int left = 0;
        int right = 0;
        int[] hash = new int[100001];
        int ret = 0;
        while (right < arr.length) {
            while (right < arr.length && hash[arr[right]]++ == 0) {
                right++;
                ret = Math.max(ret, right-left);
            }
            while (left < right && hash[arr[left]] > 1) {
                hash[arr[left++]]--;
            }
            right++;
        }
        return ret;
    }

    public static void main(String[] args) {
        Test2 test2 = new Test2();
        System.out.println(test2.maxLength(new int[] {1, 1, 2, 2 }));
    }
}
